package com.AmericanExpress.LookandFeel;

import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.admin.users.WebUser;
import com.microstrategy.web.objects.admin.users.WebUserGroup;
import com.microstrategy.web.transform.StyleMapper;
import com.microstrategy.web.transform.StyleMapperResult;
import com.microstrategy.web.transform.StyleRequestContext;
import com.microstrategy.web.transform.StyleMapperResultImpl;

public class CustomPageStyle implements StyleMapper {
	
    public StyleMapperResult mapStyle(String styleName, StyleRequestContext context, String addlData) {
    
        /* Define the default result - false
         */
        StyleMapperResultImpl defaultResult = new StyleMapperResultImpl(false, null);
        
        //Retrieve the user's session from the context
        WebIServerSession sess = context.getWebIServerSession();
        
        try {
        	
        	//Obtain the user's groups it belongs to
			WebUser user = (WebUser) sess.getUserInfo();
			WebUserGroup[] wu = user.getAncestorGroups();
			
			//Iterate through the groups
			for (int i = 0; i < wu.length; i++) {
				
				//Check if it matches the string setting additional data
				if(wu[i].getDisplayName().equalsIgnoreCase(addlData)){
					
					//Setting the default request to true causes to use the 'Map to Style'
					defaultResult.setWereConditionsMet(true);
					break;
				}
			}
		} catch (WebObjectsException e) {
			e.printStackTrace();
		}

        return defaultResult;
    }
}